package com.tqjc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.entity.SteelPlate;
import com.tqjc.mapper.SteelPlateMapper;
import com.tqjc.service.SteelPlateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 钢板信息;(steel_plate)表服务实现类
 * @author: pzy
 * @date: 2023年06月08日 16:55
 */
@Service
public class SteelPlateServiceImpl extends ServiceImpl<SteelPlateMapper,SteelPlate> implements SteelPlateService {

    @Resource
    private SteelPlateMapper steelPlateMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SteelPlate queryById(String id){
        return steelPlateMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param steelPlate 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<SteelPlate> paginQuery(SteelPlate steelPlate, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<SteelPlate> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(steelPlate.getSteelId())){
            queryWrapper.eq(SteelPlate::getSteelId, steelPlate.getSteelId());
        }
        if(StrUtil.isNotBlank(steelPlate.getSteelType())){
            queryWrapper.eq(SteelPlate::getSteelType, steelPlate.getSteelType());
        }
        if(steelPlate.getStockId()!=0){
            queryWrapper.eq(SteelPlate::getStockId, steelPlate.getStockId());
        }
        //2. 执行分页查询
        Page<SteelPlate> pagin = new Page<>(current , size , true);
        IPage<SteelPlate> selectResult = steelPlateMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param steelPlate 实例对象
     * @return 实例对象
     */
    @Override
    public SteelPlate insert(SteelPlate steelPlate){
        steelPlateMapper.insert(steelPlate);
        return steelPlate;
    }

    /**
     * 更新数据
     *
     * @param steelPlate 实例对象
     * @return 实例对象
     */
    @Override
    public SteelPlate update(SteelPlate steelPlate){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<SteelPlate> chainWrapper = new LambdaUpdateChainWrapper<SteelPlate>(steelPlateMapper);
        if(StrUtil.isNotBlank(steelPlate.getSteelId())){
            chainWrapper.eq(SteelPlate::getSteelId, steelPlate.getSteelId());
        }
        if(StrUtil.isNotBlank(steelPlate.getSteelType())){
            chainWrapper.eq(SteelPlate::getSteelType, steelPlate.getSteelType());
        }
        if(steelPlate.getStockId()!=0){
            chainWrapper.eq(SteelPlate::getStockId, steelPlate.getStockId());
        }
        //2. 设置主键，并更新
        chainWrapper.set(SteelPlate::getId, steelPlate.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(steelPlate.getId());
        }else{
            return steelPlate;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id){
        int total = steelPlateMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public List<SteelPlate> getAllSteelPlateByArea(String area) {
        return steelPlateMapper.getPlatesByArea(area);
    }
}
